package com.chat.api.web.dao;

import com.chat.api.common.BaseRepo;
import com.chat.api.web.po.ChatBean;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface ChatRepo extends BaseRepo<ChatBean,Long> {

    @Query(value = "select * from message where user_id = ?1 and receiver = ?2 ",nativeQuery = true)
    List<ChatBean> findByUidAndRid(Long userId, Long receiver);

    @Query(value = "select * from message where receiver = ?1 and user_id = ?2 ",nativeQuery = true)
    List<ChatBean> findByRidAndUid(Long userId, Long receiver);

    @Query(value = "select count(*) from message where user_id = ?1 and receiver = ?2 and is_read = 0",nativeQuery = true)
    Integer findNoReadCountById(Long userId, Long receiver);

    @Modifying
    @Transactional
    @Query(value = "update message set is_read = 1 where receiver = ?1 and user_id = ?2" ,nativeQuery = true)
    void updateNoRead(Long userId, Long receiver);

    @Query(value = "select count(*) from message where receiver = ?1 and is_read = 0",nativeQuery = true)
    Integer findAllNoReadCount(Long id);
}
